package com.example.demo1.generator.importPaper;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * https://www.gkzenti.cn/searchp?cls=%E8%BD%AF%E8%80%83&s=%E7%B3%BB%E7%BB%9F%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1%E5%B8%88
 */
public class GetPagerAPI {

//    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
//<dependency>
//    <groupId>org.apache.httpcomponents</groupId>
//    <artifactId>httpclient</artifactId>
//    <version>4.5.10</version>
//</dependency>

    /**
     * 示例
     */

    public static void main(String[] args) {
//
        new GetPagerAPI().doGet();
    }

    public String doGet() {
        String uriAPI = "https://www.gkzenti.cn/searchp?cls=%E8%BD%AF%E8%80%83&s=%E7%B3%BB%E7%BB%9F%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1%E5%B8%88";
        String result = "";
//    	HttpGet httpRequst = new HttpGet(URI uri);
//    	HttpGet httpRequst = new HttpGet(String uri);
//    	创建HttpGet或HttpPost对象，将要请求的URL通过构造方法传入HttpGet或HttpPost对象。
        HttpGet httpRequst = new HttpGet(uriAPI);

//    	new DefaultHttpClient().execute(HttpUriRequst requst);
        try {
            //使用DefaultHttpClient类的execute方法发送HTTP GET请求，并返回HttpResponse对象。
            HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);//其中HttpGet是HttpUriRequst的子类
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                HttpEntity httpEntity = httpResponse.getEntity();
                result = EntityUtils.toString(httpEntity);//取出应答字符串
                // 一般来说都要删除多余的字符
                result.replaceAll("\r", "");//去掉返回结果中的"\r"字符，否则会在结果字符串后面显示一个小方格
                //转中文字符
                List<String> stringList=new ArrayList<>();
                int length=result.length();
                int start=0;
                int end=0;
                start=result.indexOf("href=/paer");
//                while (end<length){
//                    end=result.indexOf("\">",start);
//                     result.substring(start,end);
//                }
                System.out.println("utf-8 编码：" + result) ;
            } else {
                httpRequst.abort();
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = e.getMessage().toString();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = e.getMessage().toString();
        }
        return result;

    }
}
